package com.xd.navi;

import android.net.ConnectivityManager;
import android.net.Network;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class NetworkCallbackImpl extends ConnectivityManager.NetworkCallback{

    private NetChangeListener listener;

    public NetworkCallbackImpl(NetChangeListener listener) {
        this.listener = listener;
        if(listener != null){
            if(NetworkUtils.isConnected()){
                listener.onConnect();
            }else {
                listener.onDisConnect();
            }
        }
    }

    @Override
    public void onAvailable(@NonNull Network network) {
        super.onAvailable(network);
        if(listener != null){
            listener.onConnect();
        }
    }

    @Override
    public void onLost(@NonNull Network network) {
        super.onLost(network);
        if(listener != null){
            listener.onDisConnect();
        }
    }

    public interface NetChangeListener {
        /**
         * 已连接
         */
        void onConnect();

        /**
         * 连接断开
         */
        void onDisConnect();
    }
}
